package com.jn.server;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private IStudentService mService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    @Override
    protected void onResume() {
        super.onResume();
        bindService();
    }

    private void bindService() {
        Log.d("asdf", "======================bindService======================");
        Intent intent = new Intent();
        intent.setPackage("com.jn.server");
        intent.setAction("com.jn.server.action");
        startService(intent);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = IStudentService.Stub.asInterface(service);
            Log.d("asdf", "onServiceConnected");
            try {
                Log.d("asdf", "mService.getStudentCount() === " + mService.getStudentCount());
            } catch (RemoteException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mService = null;
        }
    };
}